package com.elec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elec.domain.R;
import com.elec.mapper.AnalysisReportMapper;
import com.elec.domain.entity.AnalysisReport;
import com.elec.service.AnalysisReportService;
import org.springframework.stereotype.Service;

/**
 * 存储针对每位用户的用电分析报告和节能建议(AnalysisReport)表服务实现类
 *
 * @author yu
 * @since 2024-07-05 13:42:59
 */
@Service("analysisReportService")
public class AnalysisReportServiceImpl extends ServiceImpl<AnalysisReportMapper, AnalysisReport> implements AnalysisReportService {

    @Override
    public R add(AnalysisReport analysisReport) {
        if (save(analysisReport)){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R delete(Long id) {
        if (removeById(id)){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R get(Long id) {
        if (getById(id) != null){
            return R.success(getById(id));
        }
        return R.error("查询失败");
    }

    @Override
    public R getList(Integer pageNum, Integer pageSize, AnalysisReport analysisReport) {
        LambdaQueryWrapper<AnalysisReport> queryWrapper = new LambdaQueryWrapper<>();
        if (analysisReport != null){
            if (analysisReport.getReportId() != null){
                queryWrapper.eq(AnalysisReport::getReportId, analysisReport.getReportId());
            }
            if (analysisReport.getUserId() != null){
                queryWrapper.eq(AnalysisReport::getUserId, analysisReport.getUserId());
            }
        }
        Page<AnalysisReport> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        return R.success(page.getRecords(), page.getTotal());
    }

    @Override
    public R update(AnalysisReport analysisReport) {
        if (updateById(analysisReport)){
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }
}


